<%@ page language="java" import="java.util.*, dtt.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
  ArrayList projectBeanList = (ArrayList) request.getAttribute("projectBeanList");
  ArrayList defectList = (ArrayList) request.getAttribute("defectList");
  dtt.SearchCriteria criteria = (SearchCriteria) request.getAttribute("searchCriteria");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'Content_template.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	
	<link rel="stylesheet" type="text/css" href="styles.css">
	
<script language = "javascript">

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function validateform(){
	var dt=window.searchdefectform.dateSubmitted
	if(dt.value ==""){
	return true;
	}
	if(isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }

</script>
  </head>
  
  <body>
  <form id="searchdefectform" action="servlet/FrontController" method="post">
	  <input  type="hidden" name="DTT_ACTION" value="SEARCH_DEFECT">
    <table valign="top" border="0" align="center" width="85%">
     <tr>
       <td><b>Project</td>
       <td><b>DefectNumber</td>
       <td><b>ReportedBy</td>
     </tr>
     <tr>
       <td><select name="projectId" >       <option value="">- - -</option>
       <%
        Iterator itr = projectBeanList.iterator();
        while(itr.hasNext()){
        String selected_str = "";
        ProjectBean project = (ProjectBean) itr.next();
        if(criteria != null && criteria.getProjectId() != null) {
          if((""+project.getProjectId()).equals(criteria.getProjectId())) {
           selected_str = "selected";
          }
        }
        %>

       <option value="<%=""+project.getProjectId()%>" <%=selected_str %>><%=project.getProjectNbr()%></option>
       <%}//end while %>
       
       </select>     		
       </td>
       <%
         String temp_defect_id = "";
         if(criteria != null&& criteria.getDefectId() != null) {
           temp_defect_id = criteria.getDefectId();
         }
        %>
       <td><b><input type="text" name="defectId" value='<%=temp_defect_id %>'></td>
       <%
         String temp_reported_by = "";
         if(criteria != null&& criteria.getReportedBy() != null) {
           temp_reported_by = criteria.getReportedBy();
         }
        %>
       <td><b><input type="text" name="reportedBy" value='<%=temp_reported_by%>'></td>
     </tr>
     <tr>
       <td><b>Date Submitted </b><font size="1">(MM/DD/YYYY)</font></td>              
       <td><b>Severity</td>       
       <td><b>Priority</td>       
     </tr>
     <tr>
       <%
         String temp_date_submitted = "";
         if(criteria != null&& criteria.getDateSubmitted() != null) {
           temp_date_submitted = criteria.getDateSubmitted();
         }
        %>
       <td><b><input type="text" name="dateSubmitted" value='<%=temp_date_submitted %>'></td>
       <td><select name="severity" >
       <option value="Select">- - -</option>
       <%  String low_selected = "";
           String medium_selected = "";
           String high_selected = "";
       if(criteria != null && criteria.getSeverity() != null) {
         if(criteria.getSeverity().equals(Constants.SEVERITY_LOW)) {
           low_selected = "selected";
         }
         if(criteria.getSeverity().equals(Constants.SEVERITY_MEDIUM)) {
           medium_selected = "selected";
         }
                  if(criteria.getSeverity().equals(Constants.SEVERITY_HIGH)) {
           high_selected = "selected";
         }
        } //end criteria not null
       %>
       <option value="<%=Constants.SEVERITY_LOW%>" <%= low_selected%>><%=Constants.SEVERITY_LOW %></option>
       <option value="<%=Constants.SEVERITY_MEDIUM%>" <%= medium_selected%>><%=Constants.SEVERITY_MEDIUM %></option>
       <option value="<%=Constants.SEVERITY_HIGH%>" <%= high_selected%>><%=Constants.SEVERITY_HIGH %></option>
       </select></td>       
       <%  String lowp_selected = "";
           String mediump_selected = "";
           String highp_selected = "";
       if(criteria != null && criteria.getPriority() != null) {
         if(criteria.getPriority().equals(Constants.PRIORITY_LOW)) {
           lowp_selected = "selected";
         }
         if(criteria.getPriority().equals(Constants.PRIORITY_MEDIUM)) {
           mediump_selected = "selected";
         }
         if(criteria.getPriority().equals(Constants.PRIORITY_HIGH)) {
           highp_selected = "selected";
         }
        } //end criteria not null
       %>

       <td><select name="priority" >
       <option value="Select">- - -</option>       
       <option value="<%=Constants.PRIORITY_LOW%>" <%= lowp_selected%> ><%=Constants.PRIORITY_LOW%></option>
       <option value="<%=Constants.PRIORITY_MEDIUM%>"<%=mediump_selected%>><%=Constants.PRIORITY_MEDIUM %></option>
       <option value="<%=Constants.PRIORITY_HIGH%>"<%= highp_selected%> ><%=Constants.PRIORITY_HIGH%></option>
       </select></td>
     </tr>
     <tr>
       <td><b>AssignedTo</td>       
       <td><b>DefectStatus</td>       
       <td><b>Defect Description</td>       
     </tr>
       <%
         String temp_assigned_to = "";
         if(criteria != null&& criteria.getAssignedTo() != null) {
           temp_assigned_to = criteria.getAssignedTo();
         }
         String temp_desc= "";
         if(criteria != null&& criteria.getDescription() != null) {
           temp_desc = criteria.getDescription();
         }
         
        %>
     <tr>
      <%  String state_submitted_selected = "";
           String state_assessed_selected = "";
           String state_assigned_selected = "";
           String state_resolved_selected = "";
           String state_tested_selected = "";
           String state_closed_selected = "";
           String state_rejected_selected = "";
           String state_postponed_selected = "";
       if(criteria != null && criteria.getDefectStatus() != null) {
         if(criteria.getDefectStatus().equals(Constants.STATE_SUBMITTED)) {
           state_submitted_selected = "selected";
         }
         if(criteria.getDefectStatus().equals(Constants.STATE_ASSESSED)) {
           state_assessed_selected = "selected";
         }
         if(criteria.getDefectStatus().equals(Constants.STATE_ASSIGNED)) {
           state_assigned_selected = "selected";
         }
		if(criteria.getDefectStatus().equals(Constants.STATE_RESOLVED)) {
           state_resolved_selected = "selected";
         }
         if(criteria.getDefectStatus().equals(Constants.STATE_TESTED)) {
           state_tested_selected = "selected";
         }
         if(criteria.getDefectStatus().equals(Constants.STATE_CLOSED)) {
           state_closed_selected = "selected";
         }
         if(criteria.getDefectStatus().equals(Constants.STATE_REJECTED)) {
           state_rejected_selected = "selected";
         }
         if(criteria.getDefectStatus().equals(Constants.STATE_POSTPONED)) {
           state_postponed_selected = "selected";
         }
        } //end criteria not null
       %>
     
       <td><b><input type="text" name="assignedTo" value="<%=temp_assigned_to %>"></td>
       <td><select name="defectStatus">
       <option value="Select">- - -</option>       
       <option value="<%=Constants.STATE_SUBMITTED %>"<%=state_submitted_selected %>><%=Constants.STATE_SUBMITTED %></option>
       <option value="<%=Constants.STATE_ASSESSED %>"<%=state_assessed_selected %>><%=Constants.STATE_ASSESSED %></option>
       <option value="<%=Constants.STATE_ASSIGNED %>"<%=state_assigned_selected %>><%=Constants.STATE_ASSIGNED %></option>
       <option value="<%=Constants.STATE_RESOLVED %>"<%=state_resolved_selected %>><%=Constants.STATE_RESOLVED %></option>
      <option value="<%=Constants.STATE_TESTED %>"<%=state_tested_selected %>><%=Constants.STATE_TESTED %></option>
      <option value="<%=Constants.STATE_CLOSED %>"<%=state_closed_selected %>><%=Constants.STATE_CLOSED %></option>
      <option value="<%=Constants.STATE_REJECTED %>"<%=state_rejected_selected %>><%=Constants.STATE_REJECTED %></option>
       <option value="<%=Constants.STATE_POSTPONED %>"<%=state_postponed_selected %>><%=Constants.STATE_POSTPONED %></option>
       </select></td>       
       <td><b><input type="text" name="description" size="40" value="<%=temp_desc%>"></td>
     </tr>
	<tr></tr>
	<tr>
	  <td><input type="submit" value="Search Defects" onclick='return validateform()'>
	</tr>
  	 </table>
  	 <br><br>
<table valign="top" border="1" align="center" width="85%">

	<%
    DefectBean defect = null;
    if(defectList != null){
    %>
	<tr><th colspan="5">Results</th></tr>
	<tr>
	<th> Project Id </th>
	<th>Defect Id </th>
	<th>Defect Description </th>
	<th>Defect Severity </th>
	<th> Defect Status </th> 
	</tr>
      <%
        Iterator itr1 = defectList.iterator();
        while(itr1.hasNext()){
        defect = (DefectBean)itr1.next();
     %>
        <tr> 
        <td > <a><%= defect.getProjectId() %> </a></td>
        <td><a href="servlet/FrontController?DTT_ACTION=VIEW_DEFECT&defectId=<%=defect.getDefectId()%>&projectId=<%=defect.getProjectId()%>"><%= defect.getDefectId() %> </td>
        
        <td> <%= defect.getDefectDesc() %></td>
        <td> <%= defect.getDefectSeverity() %> </td>
        <td> <%= defect.getDefectStatus() %> </td>
        </tr>
    <%
        } //end while
     } //end if
     %>
  </table>
  </form>
  </body>
</html>
	